package com.sci.controller;

import com.sci.entity.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    @Value("${server.port}")
    private int serverPort;

    @GetMapping("/{id}")
    public User getUser(@PathVariable(value = "id") Integer id) {
        logger.info("服务正在端口: {} 上运行", serverPort);

        User user = new User();
        user.setId(id);
        user.setName("小明");
        user.setPhoneNumber("1388888888");
        return user;
    }


    // POST：创建用户
    @PostMapping
    public String createUser(@RequestBody User user) {
        logger.info("创建用户: {}", user.getName());
        user.setId((int)(Math.random() * 1000)); // 模拟数据库生成ID
        return "用户创建成功！";
    }

    // PUT：更新用户
    @PutMapping("/{id}")
    public String updateUser(@PathVariable(value = "id") Integer id, @RequestBody User user) {
        logger.info("用户ID为{}的用户信息更新成功", id);
        user.setId(id); // 确保ID一致
        return "用户ID为"+id+"的用户信息更新成功";
    }

    // DELETE：删除用户
    @DeleteMapping("/{id}")
    public String deleteUser(@PathVariable(value = "id") Integer id) {
        logger.info("用户ID为{}的用户信息删除成功", id);
        return "用户ID为"+id+"的用户信息删除成功";
    }
}
